# -*- coding: utf-8 -*-
import sys

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import Qt
from PySide6.QtWidgets import (QApplication, QHeaderView, QMainWindow, QMenuBar,
                               QPushButton, QSizePolicy, QStatusBar, QTableWidget,
                               QTableWidgetItem, QWidget, QVBoxLayout, QFormLayout, QLineEdit,QCheckBox)
from uis.add_stu import AddStu
from Qt0918.db.qt_mysql import MyMySQL
class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi()
        self.table = QTableWidgetItem


    def setupUi(self):
        self.resize(800, 600)
        self.setWindowTitle("学籍管理")
        # 创建垂直布局
        v_layout = QVBoxLayout(self)
        v_layout.setContentsMargins(200, 100, 200, 0)


        # 创建表格
        self.sql = MyMySQL()
        # 展示数据
        users = self.sql.select()
        print("查询结果>>", users)
        row = len(users)
        col = len(users[0])
        print(">>>>", row, col)

        # 设置表头
        self.table_widget = QTableWidget(row, col+1, self)
        self.table_widget.setHorizontalHeaderLabels(['id', '姓名', '年龄', '手机号', '学制', '学籍', '状态'])
        # 填充数据  row_index行索引  i数据
        for row_index, i in enumerate(users):
            print(i, row_index)
            for index, j in enumerate(i):
                item = QTableWidgetItem(str(j))
                self.table_widget.setItem(row_index, index, item)
                # 在第一行的最后一列添加选择框
                self.table_widget.setCellWidget(row_index, col,  QCheckBox())



        del_btn = QPushButton("删除")
        add_btn = QPushButton("增加")
        # 槽函数
        add_btn.clicked.connect(self.add_stu)
        # 删除要选择勾选的状态
        self.ids = []
        for row_index, i in enumerate(users):
            item = QTableWidgetItem(row_index)
            # print(item.isSelected(), i, row_index)   # False
            if item.isSelected():
                # 把选中行的数据放入列表中
                self.ids.append(i[0])
            print(self.ids)

        del_btn.clicked.connect(self.del_stu)
        v_layout.addWidget(add_btn)
        v_layout.addWidget(del_btn)
        self.table_widget.setLayout(v_layout)
        self.setCentralWidget(self.table_widget)



    # def setupUi(self, MainWindow):
    #     if not MainWindow.objectName():
    #         MainWindow.setObjectName(u"MainWindow")
    #     MainWindow.resize(802, 600)
    #     self.centralwidget = QWidget(MainWindow)
    #     self.centralwidget.setObjectName(u"centralwidget")
    #     self.tableWidget = QTableWidget(self.centralwidget)
    #     if (self.tableWidget.columnCount() < 6):
    #         self.tableWidget.setColumnCount(6)
    #     __qtablewidgetitem = QTableWidgetItem()
    #     self.tableWidget.setHorizontalHeaderItem(0, __qtablewidgetitem)
    #     __qtablewidgetitem1 = QTableWidgetItem()
    #     self.tableWidget.setHorizontalHeaderItem(1, __qtablewidgetitem1)
    #     __qtablewidgetitem2 = QTableWidgetItem()
    #     self.tableWidget.setHorizontalHeaderItem(2, __qtablewidgetitem2)
    #     __qtablewidgetitem3 = QTableWidgetItem()
    #     self.tableWidget.setHorizontalHeaderItem(3, __qtablewidgetitem3)
    #     __qtablewidgetitem4 = QTableWidgetItem()
    #     self.tableWidget.setHorizontalHeaderItem(4, __qtablewidgetitem4)
    #     __qtablewidgetitem5 = QTableWidgetItem()
    #     self.tableWidget.setHorizontalHeaderItem(5, __qtablewidgetitem5)
    #     self.tableWidget.setObjectName(u"tableWidget")
    #     self.tableWidget.setGeometry(QRect(60, 30, 611, 511))
    #     self.tableWidget.setStyleSheet(u"background-color :rgb(189, 198, 196)")
    #     self.del_btn = QPushButton(self.centralwidget)
    #     self.del_btn.setObjectName(u"pushButton")
    #     self.del_btn.setGeometry(QRect(130, 440, 461, 24))
    #     self.add_btn = QPushButton(self.centralwidget)
    #     self.add_btn.setObjectName(u"pushButton_2")
    #     self.add_btn.setGeometry(QRect(130, 480, 461, 24))
    #     MainWindow.setCentralWidget(self.centralwidget)
    #     self.menubar = QMenuBar(MainWindow)
    #     self.menubar.setObjectName(u"menubar")
    #     self.menubar.setGeometry(QRect(0, 0, 802, 33))
    #     MainWindow.setMenuBar(self.menubar)
    #     self.statusbar = QStatusBar(MainWindow)
    #     self.statusbar.setObjectName(u"statusbar")
    #     MainWindow.setStatusBar(self.statusbar)
    #
    #     self.retranslateUi(MainWindow)
    #
    #     QMetaObject.connectSlotsByName(MainWindow)
    #
    #
    #     # 设置点击事件
    #     self.del_btn.clicked.connect(self.del_stu)
    #     self.add_btn.clicked.connect(self.add_stu)
    # setupUi

    def add_stu(self):
        """添加学生信息"""
        self.add_page = AddStu()
        self.add_page.show()

    def del_stu(self):
        """删除学生信息"""
        self.selected_list = []
        for i in range(self.table_widget.rowCount()):
            checkbox = self.table_widget.cellWidget(i, 6)
            if checkbox.isChecked():
                self.selected_list.append(i)
        print(self.selected_list)

        for j in self.selected_list:
            id = self.table_widget.item(j, 0).text()
            sql = f" DELETE FROM Student WHERE id={id}"
            self.sql.del_student(sql)
        self.setupUi()




    # def retranslateUi(self, MainWindow):
    #     MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
    #     ___qtablewidgetitem = self.tableWidget.horizontalHeaderItem(0)
    #
    #     ___qtablewidgetitem.setText(QCoreApplication.translate("MainWindow", "状态", None));
    #     ___qtablewidgetitem1 = self.tableWidget.horizontalHeaderItem(1)
    #     ___qtablewidgetitem1.setText(QCoreApplication.translate("MainWindow", "姓名", None));
    #     ___qtablewidgetitem2 = self.tableWidget.horizontalHeaderItem(2)
    #     ___qtablewidgetitem2.setText(QCoreApplication.translate("MainWindow", "年龄", None));
    #     ___qtablewidgetitem3 = self.tableWidget.horizontalHeaderItem(3)
    #     ___qtablewidgetitem3.setText(QCoreApplication.translate("MainWindow", "手机号", None));
    #     ___qtablewidgetitem4 = self.tableWidget.horizontalHeaderItem(4)
    #     ___qtablewidgetitem4.setText(QCoreApplication.translate("MainWindow", "学制", None));
    #     ___qtablewidgetitem5 = self.tableWidget.horizontalHeaderItem(5)
    #     ___qtablewidgetitem5.setText(QCoreApplication.translate("MainWindow", "学籍", None));
    #     self.del_btn.setText(QCoreApplication.translate("MainWindow", u"\u5220\u9664", None))
    #     self.add_btn.setText(QCoreApplication.translate("MainWindow", u"\u6dfb\u52a0", None))
    # # retranslateUi

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Ui_MainWindow()
    window.show()
    sys.exit(app.exec())